#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <stdbool.h>
#include <pthread.h>
#include <semaphore.h>

#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/fb.h>
#include <linux/un.h>

#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>

#include "common.h"
#include "cJSON.h"

/*-------车牌识别-------*/


//有名管道
#define BASE64_CARID "/tmp/BASE64_CARID"
#define SQLite_CARID "/tmp/SQLite_CARID"
extern int h_errno;

char *http_request(char *pic)
{
	static char req[1024*1024*2+500];
	bzero(req, 1024*1024*2+500);

	snprintf(req,1024*1024*2+500, "POST /ocr/car-license?pic=%s HTTP/1.1\r\n"
			   "Host: api03.aliyun.venuscn.com\r\n"
			   "Authorization: APPCODE 431922883a3f432a96f4d57e3a05fa7c\r\n\r\n",
			   strtok(pic, "\n"));
			 
	return req;
}

int get_size(char *head)
{
	char *p;
	if((p=strstr(head, "Content-Length:")) != NULL)
	{
		return atoi(p+strlen("Content-Length: "));
	}
}

int main(int argc, char **argv)
{
	
	//创建或打开有名管道
	
	printf("成功调用 CARID_demo程序\n");
	
	int fifo_BASE64_CARID = open(BASE64_CARID, O_RDWR);
	int fifo_SQLite_CARID = open(SQLite_CARID, O_RDWR);
	
	char *pic = calloc(1,1024*1024*2);
	
	
	bzero(pic,1024*1024*2);
	read(fifo_BASE64_CARID,pic,1024*1024*2);
	
	//printf("收到base64发来的管道数据： %s\n",pic);
	printf("\n\n\n");
	
	// 1，取得服务器的IP
	struct hostent *he = gethostbyname("api03.aliyun.venuscn.com");
	if(he == NULL)
	{
		printf("获取服务器IP失败: %s\n", strerror(h_errno));
		exit(0);
	}

	char *ip = inet_ntoa(*((struct in_addr *)(he->h_addr_list[0])));
	printf("%s\n", ip);
	//112.124.219.219


	// 2，试图连接服务器
	int sockfd = Socket(AF_INET, SOCK_STREAM, 0);

	struct sockaddr_in addr;
	socklen_t len = sizeof(addr);
	bzero(&addr, len);

	addr.sin_family = AF_INET;
	addr.sin_addr = *((struct in_addr *)(he->h_addr_list[0]));
	addr.sin_port = htons(80);

	Connect(sockfd, (struct sockaddr *)&addr, len);
	printf("连接成功！\n");


// 3，组织好HTTP的请求报文，并发送给服务器
//	printf("图片base64数据:\n");
//	char image[1024];
//	bzero(image, 1024);
//	fgets(image, 1024, stdin);

	
		
	char *req = http_request(pic);

	//printf("请求报文:\n%s", req);

	    int len_req = strlen(req);
	    int m;
	    while(len_req > 0)
	    {
		   m = write(sockfd, req, strlen(req));
		   len_req -= m;
		   req += m;
	    }
	    printf("发送请求报文成功！\n");



	    // 4，静静地等待服务器的响应，并判断成功与否
	    char response[1024];
	    bzero(response, 1024);

	    // 4.1 读取响应头部
	    for(int i=0; ; i++)
	    {
		   read(sockfd, response+i, 1);

		   if(strstr(response, "\r\n\r\n"))
			  break;
	    }
//	printf("响应头部:\n%s", response);

	    // 4.2 读取JSON
	    int len_json = get_size(response);
	    printf("JSON数据长度:%d\n", len_json);

	    char *json = calloc(1, len_json);
	    int total = 0;
	    while(len_json > 0)
	    {
		    int n = read(sockfd, json+total, len_json);
		    len_json -= n;
		    total += n;
	    }
	    printf("JSON:\n%s", json);

	// 5，分析JSON数据
	    cJSON *root = cJSON_Parse(json);
	    cJSON *data = cJSON_GetObjectItem(root, "data");
	    char *number = cJSON_GetObjectItem(data, "number")->valuestring;	
	
	    printf("\n\n");
	    printf("识别的车牌: %s\n", number);
	
	    //将识别出来的车牌号发送给数据库程序
        int n = write(fifo_SQLite_CARID,number,strlen(number));
		printf("成功向数据库管道写入 %d 字节\n",n);
	
	    return 0;
}
